package main

import (
	"fmt"
)

/**
   	1.方括号中是key的类型,外面是value的类型
	2.除了slice,map,function的内建类型都可以作为Key
	3.Struct类型不包含上诉字段，也可以作为key(编译的时候进行检查Struct是否含有上述字段)
 */
func main() {

	m := map[string]string{
		"name":    "alex",
		"course":  "golang",
		"site":    "snqu",
		"quality": "good",
	}

	//mx := map[int]string{
	//	111: "12",
	//}

	m2 := make(map[string]int)

	var m3 map[string]int

	fmt.Println("m = ", m)
	fmt.Println("m2 = ", m2)
	fmt.Println("m3 = ", m3)

	fmt.Println("Traversing map")

	/**
		这个map是一个hashmap, 里面的数据是无序的
	 */
	for k, v := range m {
		fmt.Println(k, v)
	}

	fmt.Println("Getting values")

	//获得courseName
	//courseName := m["course"]
	courseName, ok := m["course"]
	fmt.Println(courseName, ok)

	//如果没有找到对应的key,则返回空字符串
	//这里ok的作用是boolean,用于判断key是否存在于map
	causeName, ok := m["cause"]
	fmt.Println(causeName, ok)

	if causeName, ok := m["cause"]; ok {
		fmt.Println(causeName)
	} else {
		fmt.Println("key does not exsit")
	}

	fmt.Println("Deleting values")
	name, ok := m["name"]
	fmt.Println(name, ok)
	delete(m, "name1")
	fmt.Println("after deleting map m")

	for key, value := range m {
		fmt.Println(key, value)
	}
}
